package com.songhq.data.log.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Lazy(false)
@Order(1)
public class ApplicationContextRegister
  implements ApplicationContextAware
{
  private static ApplicationContext applicationContext;

  public void setApplicationContext(ApplicationContext applicationContext)
    throws BeansException
  {
    applicationContext = applicationContext;
  }

  public static Object getBean(String name) throws BeansException {
    if (null == applicationContext) {
      return null;
    }
    return applicationContext.getBean(name);
  }

  public static <T> T getBean(Class<T> clazz) {
    if (null == applicationContext) {
      return null;
    }
    return applicationContext.getBean(clazz);
  }

  public static <T> T getBean(String name, Class<T> clazz) {
    if (null == applicationContext) {
      return null;
    }
    return applicationContext.getBean(name, clazz);
  }
}